package cn.com.yusys.yusp.admin.web.rest;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.com.yusys.yusp.admin.domain.AdminSmLogicSys;
import cn.com.yusys.yusp.admin.domain.AdminSmLogicSysVO;
import cn.com.yusys.yusp.admin.service.AdminSmLogicSysService;
import cn.com.yusys.yusp.commons.exception.MessageConstants;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.web.rest.CommonResource;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;

/**
 * @version 1.0.0
 * @项目名称: yusp-admin
 * @类名称: AdminSmLogicSyResource
 * @类描述: 逻辑管理系统接口层
 * @功能描述:
 * @创建人: dusong@yusys.com.cn
 * @创建时间: 2017-12-19 13:41
 * @修改备注:
 * @修改记录: 修改时间    修改人员    修改原因
 * -------------------------------------------------------------
 * @Copyright (c) 2017宇信科技-版权所有
 */
@RestController
@RequestMapping("/api/adminsmlogicsys")
public class AdminSmLogicSysResource extends CommonResource<AdminSmLogicSys, String> {

    @Autowired
    private AdminSmLogicSysService adminSmLogicSyService;

    @Override
    protected CommonService getCommonService() {
        return this.adminSmLogicSyService;
    }

    @PostMapping("/delete")
    protected ResultDto<Integer> delete(@RequestParam("id") String id) {
        ResultDto<Integer> resultDto = new ResultDto<>();

        //调用父类接口查询数据
        ResultDto<Object> r = super.show(id);
        //如果根据ID查到的逻辑系统中状态为生效不能删除 modify at 2018.12.06
        if(null != r && null != r.getData() && ((AdminSmLogicSys)r.getData()).getSysSts().equals("A") ){
            resultDto.setMessage("生效状态的逻辑系统不能删除");
        }else {
            int result = this.adminSmLogicSyService.deleteLogicAndCrelInfo(id);
            String sysId = id;

            resultDto.setData(result);
            if (result >= 1) {
                resultDto.setTotal(result);
                resultDto.setMessage("删除逻辑系统,sysId:{" + sysId + "}" + " , " + "result:{" + (result == 1 ? "success" : "failed") + "}");
            }
        }
        return resultDto;
    }

    /**
     * @方法名称: getLogicSysKeyValue
     * @方法描述: 逻辑系统列表
     * @参数与返回说明:
     * @算法描述:
     */
    @GetMapping("/logicsyskv")
    protected ResultDto<List<Map<String, String>>> getLogicSysKeyValue() {
        List<Map<String, String>> list = this.adminSmLogicSyService.getLogicSysKeyValue();
        return new ResultDto<List<Map<String, String>>>(list);
    }

    /**
     * @方法名称: createAndCopy
     * @方法描述: 初始化逻辑系统
     * @参数与返回说明:
     * @算法描述:
     */
    @PostMapping("/copy")
    protected ResultDto<AdminSmLogicSys> createAndCopy(@RequestBody AdminSmLogicSysVO t) throws Exception {
        ResultDto<AdminSmLogicSys> resultDto = new ResultDto<>();
        resultDto.setData(adminSmLogicSyService.insertAndCopy(t));
        resultDto.setMessage("成功创建逻辑系统");
        return resultDto;
    }

    /**
     * @方法名称: updateStatu
     * @方法描述: 状态修改
     * @参数与返回说明:
     * @算法描述:
     */
    @PostMapping("/updatestatu")
    protected ResultDto<Integer> updateStatu(@RequestBody AdminSmLogicSys t) throws Exception {
        ResultDto<Integer> resultDto = new ResultDto<>();
        List<Object> i18nData = new ArrayList<Object>();
        i18nData.add(t.getSysId());
        //后台控制已生效不能再次生效，已失效不能再次失效 modify at 2018.12.06
        ResultDto<Object> r = super.show(t.getSysId());
        if(null != r && null != r.getData() && ((AdminSmLogicSys)r.getData()).getSysSts().equals(t.getSysSts())){
            if(t.getSysSts().equals("A")) {
            	resultDto.setCode(MessageConstants.RET_CODE_20100017);
            	resultDto.setMessage("逻辑系统sysId: " + t.getSysId() + " 已生效,不能再次生效");
            }else {
            	resultDto.setCode(MessageConstants.RET_CODE_20100018);
            	resultDto.setMessage("逻辑系统sysId: " + t.getSysId() + " 已失效,不能再次失效");
            }
            resultDto.setI18nData(i18nData);
            return resultDto;
        }

        int result = adminSmLogicSyService.updateSelective(t);
        if (t.getSysSts().equals("A") && result >= 1) {
            resultDto.setCode(MessageConstants.RET_CODE_20100019);
        	resultDto.setMessage("逻辑系统sysId: " + t.getSysId() + " 已生效");
        }
        if (t.getSysSts().equals("I") && result >= 1) {
        	resultDto.setCode(MessageConstants.RET_CODE_20100020);
            resultDto.setMessage("逻辑系统sysId:" + t.getSysId() + "已失效");
        }
        resultDto.setI18nData(i18nData);
        resultDto.setData(result);
        resultDto.setTotal(result);
        return resultDto;
    }

    /**
     * @方法名称: queryIndex
     * @方法描述: 编辑逻辑系统时查询可设置的首页
     * @参数与返回说明:
     * @算法描述:
     */
    @GetMapping("/queryindex")
    protected ResultDto<List<Map<String, String>>> queryIndex(QueryModel model) {
        List<Map<String, String>> list = this.adminSmLogicSyService.queryIndex(model);
        return new ResultDto<List<Map<String, String>>>(list);
    }

    /**
     * @方法名称: queryIndex
     * @方法描述: 修改逻辑系统
     * @参数与返回说明:
     * @算法描述:
     */
    @PostMapping("/update/{funcId}")
    protected ResultDto<Integer> update(@RequestBody AdminSmLogicSys t, @PathVariable String funcId) {
        ResultDto<Integer> resultDto = new ResultDto<>();
        int result = adminSmLogicSyService.updateSysInfo(t, funcId);
        if (result >= 1) {
            resultDto.setMessage("修改成功");
        }
        resultDto.setData(result);
        return resultDto;

    }
}
